<?php


namespace App\Model;


use App\Service\Cache\RegionCache;
use App\Utils\Upload\View;
use Carbon\Carbon;
use Hyperf\Database\Model\Collection;

/**
 * Class Cooperative
 * @package App\Model
 * @property int|null id
 * @property mixed|void|null wx_mp_appid
 * @property mixed|void|null nickname
 * @property mixed|string|void|null wx_mp_name
 * @property int|mixed|void|null bind_wx_merchant
 * @property int|mixed|void|null bind_mp
 * @property int|mixed|void|null status
 * @property mixed|string|void|null phone
 * @property mixed|string|void|null domain
 * @property mixed|string|void|null wx_mp_secret
 * @property mixed|void|null area
 * @property mixed|void|null city
 * @property mixed|void|null province
 * @property mixed|void|null wx_cert
 * @property mixed|void|null wx_merchant_code
 * @property mixed|void|null wx_merchant_secret
 * @property int|mixed|void|null audit_status
 * @property string|mixed|void|null audit_message
 * @property string|mixed|void|null wx_mp_audit_message
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|string|void|null contact
 * @property Collection cooperativeMenus
 * @property Carbon created_at
 * @property string regionString
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|void|null logo
 * @property-read string clientUrl
 * @property Collection cooperativeUsers
 *
 */
class Cooperative extends Model
{
    protected $table = 'cooperative';
    protected $primaryKey = 'id';

    protected $hidden = [];

    public function cooperativeMenuConfigs()
    {
        return $this->belongsToMany(
            CooperativeMenuConfig::class,
            'cooperative_menu',
            'cooperative_id',
            'menu_config_id'
        );
    }

    public function cooperativeRoles()
    {
        return $this->hasMany(CooperativeRole::class);
    }

    public function cooperativeMenus()
    {
        return $this->hasMany(CooperativeMenu::class);
    }

    public function configOthers()
    {
        return $this->hasMany(ConfigOther::class);
    }

    public function tasks()
    {
        return $this->hasMany(Task::class);
    }

    public function users()
    {
        return $this->hasMany(User::class);
    }

    public function employers()
    {
        return $this->hasMany(UserEmployer::class);
    }

    public function getRegionStringAttribute()
    {
        $regionCache = di(RegionCache::class);
        $provinceName = $this->attributes['province'] ? $regionCache->get($this->attributes['province'])['sname'] : '暂无';
        $cityName = $this->attributes['city'] ? $regionCache->get($this->attributes['city'])['sname'] : '暂无';
        return $provinceName == $cityName ? $provinceName : $provinceName . $cityName;
    }

    public function getClientUrlAttribute()
    {
        return env('H5_BASE_URL').$this->domain;
    }

    public function getLogoAttribute($value)
    {
        if ($value) {
            return di(View::class)->fullPath($value);
        } else {
            return '';
        }
    }

    public function cooperativeUsers(){
        return $this->hasMany(CooperativeUser::class);
    }
}